import type { RouteObject } from "react-router-dom";
import LazyLoadAndPermission from "../util/LazyLoadAndPermission";
import { messagePage } from "@/router/page/index.ts";
import { getGroupDetil } from "@/api/xhr/message";
import { getFriend } from "@/api/xhr/friends/index";
import { getStoreItem } from "@/util/storage";
const message: RouteObject[] = [
  {
    path: "message",
    element: LazyLoadAndPermission(messagePage.Message, "message", true, true),
    children: [
      {
        path: "detail/:contactId",
        element: LazyLoadAndPermission(
          messagePage.MessageDetail,
          "messagedetail",
          true,
          true
        ),
        loader: async ({ params }) => {
          const userId = JSON.parse(getStoreItem("userinfo"))?.id;
          const data = params.contactId?.split("-");
          const type = data && (data[0] as "group" | "friend");
          const id = data && Number(data[1]);
          let detail: any;
          if (type == "friend") {
            detail = getFriend({
              messageId: id!,
              userId: userId
            });
          } else {
            detail = getGroupDetil({
              messageId: id!,
              userId: userId
            });
          }
          const [detailres] = await Promise.all([detail]);
          if (detailres?.statusCode == 200) {
            return {
              isok: 1,
              type: type,
              detail: detailres.data
            };
          }
          return {
            isok: 0,
            type: type,
            detail: detailres.data
          };
        }
      }
    ]
  }
];

export default message;
